package medium;

public class Lc2212 {
    class Solution {
        private int ret = 0;

        private int[] bob = new int[12];

        private int[] bobRet = new int[12];

        private int[] alice;

        public int[] maximumBobPoints(int numArrows, int[] aliceArrows) {
            this.alice = aliceArrows;
            this.dfs(numArrows, 0);
            // System.out.println(this.ret);
            // System.out.println(Arrays.toString(this.alice));
            return this.bobRet;
        }

        private void dfs(int arrowsCnt, int index) {
            // 边界
            if (arrowsCnt <= 0 || index >= 12) {
                int score = this.getBobScore(this.bob, this.alice);
                if (score > this.ret) {
                    this.ret = score;
                    this.bobRet = this.bob.clone();

                    if (arrowsCnt > 0) {
                        this.dealAllArrows(arrowsCnt);
                    }
                }
                return;
            }

            // 选，剩余arrow数量足够才能选
            int needArrow = this.alice[index] + 1;
            if (arrowsCnt >= needArrow) {
                this.bob[index] = needArrow;
                dfs(arrowsCnt - this.bob[index], index + 1);
            }
            this.bob[index] = 0;

            // 不选
            dfs(arrowsCnt, index + 1);
        }

        private int getBobScore(int[] bob, int[] alice) {
            int ret = 0;
            for (int i = 0; i < bob.length; i++) {
                if (bob[i] > alice[i]) {
                    ret += i;
                }
            }
            return ret;
        }

        private void dealAllArrows(int arrowsCnt) {
            for (int i = 0; i < this.bobRet.length; i++) {
                if (this.bobRet[i] > this.alice[i]) {
                    this.bobRet[i] += arrowsCnt;
                    return;
                }
            }
        }
    }
}
